clear
set more off

use "$data/oisl_clean_data.dta", clear

tab strata, gen(strata_)
tab area, gen(area_)
rename area_2 ashanti
recode cycle_base (51 52 = .)

label var female "Female"
label var age "Age"
label var age_100 "$\text{Age } / 100$"
label var accountbalance_base "Savings Balance"
label var accra "Branch in Accra"
label var ashanti "Branch in Ashanti"
label var brong "Branch in Brong"
label var central "Branch in Central Ghana"
label var eastern "Branch in Eastern Ghana"
label var western "Branch in Western Ghana"
label var loan "Loan Client"


label var amountdisbursed_base "Amount Disbursed"
label var outstandingprincipalbalance_base "Outstanding Principal Balance"
label var principalinarears_base "Principal in Arrears"
label var totalprincipaloutstanding_base "Total Principal Outstanding"
label var interestinarrears_base "Interest in Arrears"
label var pddays_base "Days in Arrears"
label var acctbalance_base "Loan Account Balance"
label var cycle_base "Loan Cycle"


// Variables regressed on treatment assignment

global vars1 female age accountbalance_base  
global vars2 ashanti accra brong central eastern western 
global vars3 loan amountdisbursed_base outstandingprincipalbalance_base /*
*/			acctbalance_base cycle_base late_repayment_base  /*
*/			principalinarears_base interestinarrears_base pddays_base 			 

est clear
local i = 1
foreach var in $vars1{
	
	eststo  model`i': quietly reg `var' treatment_2 treatment_3 strata_2 strata_3 strata_4 if month == 8
	
	test treatment_2 = treatment_3 = 0
	estadd scalar r(p)
	
	quietly summ `var' if (treatment == 0  & month == 8)
	estadd scalar r(mean)
	estadd scalar r(sd)

	local i = `i' + 1
}

foreach var in $vars2{
	
	eststo  model`i': quietly reg `var' treatment_2 treatment_3 if month == 8
		
	quietly summ `var' if (treatment == 0 & month == 8)
	estadd scalar r(mean)
	estadd scalar r(sd)

	local i = `i' + 1
}

foreach var in $vars3{
	
	eststo  model`i': quietly reg `var' treatment_2 treatment_3 /*
	*/				  strata_4 if month == 8
	
	test treatment_2 = treatment_3 = 0
	estadd scalar r(p)

	quietly summ `var' if (treatment == 0  & month == 8)
	estadd scalar r(mean)
	estadd scalar r(sd)
	
	local i = `i' + 1
}

esttab, se nostar r2 b(3) se(3) scalars("mean Mean" "sd SD" "p p-value") sfmt(%12.0fc %12.2fc )
matrix C = r(coefs)
matrix S = r(stats)
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}

local snames : rownames S
local i 0
foreach name of local snames {
    local ++i
    local j 0
    capture matrix drop b
    foreach model of local models {
        local ++j
        matrix tmp = S[`i', `j']
        matrix colnames tmp = `model'
        matrix b = nullmat(b), tmp
    }
    ereturn post b
    eststo `name'
}



esttab mean sd treatment_2 treatment_3 p N /*
*/	using "${output}/treatment balance.tex", replace/*
*/  mtitle("Control Mean" "SD" "Placebo" "Treatment" /*
*/		   "p-value" "N") /*
*/	coeflabels(model1 "\hspace{0.5cm} Female" /*
*/			   model2 "\hspace{0.5cm} Age" /*
*/			   model3 "\hspace{0.5cm} Savings Account Balance" /*
*/			   model4 "\hspace{0.5cm} Branch in Accra$^\dagger$"/*
*/			   model5 "\hspace{0.5cm} Branch in Ashanti Region$^\dagger$"/*
*/			   model6 "\hspace{0.5cm} Branch in Brong Region$^\dagger$"/*
*/			   model7 "\hspace{0.5cm} Branch in Central Region$^\dagger$"/*
*/			   model8 "\hspace{0.5cm} Branch in Eastern Region$^\dagger$"/*
*/			   model9 "\hspace{0.5cm} Branch in Western Region$^\dagger$"/*
*/			   model10 "\hspace{0.5cm} Has a Loan$^\dagger$"/*
*/			   model11 "\hspace{0.5cm} Amount Disbursed" /*
*/			   model12 "\hspace{0.5cm} Outstanding Principal Balance" /*
*/			   model13 "\hspace{0.5cm} Saving Account Balance" /*
*/			   model14 "\hspace{0.5cm} Loan Cycle" /* 
*/				model15 "\hspace{0.5cm} Late Loan" /*
*/			   model16 "\hspace{0.5cm} Principal in Arrears" /*
*/			   model17 "\hspace{0.5cm} Interest in Arrears" /*
*/			   model18 "\hspace{0.5cm} Days in Arrears") /*

*/	refcat(model1 "\emph{Panel A: Individual Characteristics}" /*
*/		   model10 "\emph{Panel B: Loan Variables}", nolabel)/*
*/	star(* 0.1 ** 0.05 *** 0.01) noobs b(2) se(2) sfmt(%9.0g)/*
*/	compress numb booktabs alignment(c) /*	
*/	title("Balance on Demographic and Loan Variables \label{tab:balance}")


reg treatment_2 $vars1 $vars2 $vars3, robust
reg treatment_3 $vars1 $vars2 $vars3, robust
